package com.gdufe.firesafe.controller;

import com.gdufe.firesafe.annotation.NeedInspector;
import com.gdufe.firesafe.annotation.NeedOwner;
import com.gdufe.firesafe.model.dto.InspectionDTO;
import com.gdufe.firesafe.model.entity.InspectionEntity;
import com.gdufe.firesafe.model.entity.UserEntity;
import com.gdufe.firesafe.service.InspectionService;
import com.gdufe.firesafe.utils.PageUtils;
import com.gdufe.firesafe.utils.R;
import com.gdufe.firesafe.utils.TokenUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;

import java.util.Arrays;
import java.util.Map;



/**
 * 安全检查记录表
 *
 * @author Liu
 * @email
 * @date 2024-03-26 16:34:06
 */
@RestController
@RequestMapping("inspection/inspection")
public class InspectionController {
    @Autowired
    private InspectionService inspectionService;

//    /**
//     * 获取业主房屋对应的检查记录
//     */
//    @NeedOwner
//    @GetMapping("/getOwnerInspection")
//    public R getOwnerInspection(){
//        return R.ok().put("data",inspectionService.getOwnerInspection());
//    }

    /**
     * 根据当前业主最新的安全检查，提供AI建议
     * @return
     */
    @GetMapping("/owner/aiSuggestion/{inspectionId}")
    @NeedOwner
    public SseEmitter getOwnerAISuggestion(@PathVariable Integer inspectionId) {
        return inspectionService.getOwnerAISuggestion(inspectionId);
    }

    /**
     * 获取单个检查员或业主的检查记录
     */
    @GetMapping("/getCurrentUserInspection")
    public R getCurrentUserInspection(@RequestParam Map<String, Object> params){
        PageUtils inspectionPage = inspectionService.getCurrentUserInspection(params);
        return R.ok().put("data", inspectionPage);
    }

    /**
     * 上传安全检查记录
     */
    @NeedInspector
    @PostMapping("/uploadInspection")
    public R uploadInspection(@RequestBody InspectionDTO inspectionDTO){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        inspectionService.uploadInspection(inspectionDTO,currentUser.getRoleId());
        return R.ok();
    }

    /**
     * 获取检查员统计页数据
     */
    @NeedInspector
    @GetMapping("/getInspectionStatistics")
    public R getInspectionStatistics(){
        UserEntity currentUser = TokenUtils.getCurrentUser();
        return R.ok().put("data",inspectionService.getInspectionStatistics(currentUser.getRoleId()));
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = inspectionService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		InspectionEntity inspection = inspectionService.getById(id);

        return R.ok().put("inspection", inspection);
    }

    /**
     * 保存
     */
    @NeedInspector
    @RequestMapping("/save")
    public R save(@RequestBody InspectionEntity inspection){
		inspectionService.save(inspection);

        return R.ok();
    }

    /**
     * 修改
     */
    @NeedInspector
    @RequestMapping("/update")
    public R update(@RequestBody InspectionEntity inspection){
		inspectionService.updateById(inspection);

        return R.ok();
    }

    /**
     * 删除
     */
    @NeedInspector
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		inspectionService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
